package com.pinoc.demo.exception;

import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.pinoc.demo.common.response.Response;
import com.pinoc.demo.enums.ReturnCode;

/**
 * @author: yinpeng10
 */
@RestControllerAdvice
public class ExceptionControllerAdvice {

	@ExceptionHandler(MethodArgumentNotValidException.class)
	public Response<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
		ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
		return new Response<>(ReturnCode.VALIDATE_FAILED, objectError.getDefaultMessage());
	}

	@ExceptionHandler(ApiException.class)
	public Response<String> ApiExceptionHandler(ApiException e) {
		return new Response<>(ReturnCode.FAILED, e.getMessge());
	}
}
